import { Modal,Toast } from 'antd-mobile'
import useApp from "@/stores/useApp";
import { API,REDIRECT_TITLE,REDIRECT_URL, REDIRECT_COVER,REDIRECT_DESC,BASE_PATH,MINI_P_ID,REDIRECT_WX_PARAMS,MINI_P_PATH,REDIRECT_WX_URL } from "@/config";
import callFeedback from '../modalFeedback';
import requewt from '@/utils/requewt';

export default () => {
    // const [env, setENV] =
    const env = useApp(state => state.env)
    const handleAppShareToH5 = (params) => {
        return new Promise((resolve,reject) => {
            // 记录分享
          if (env === `app`) {
            //
            const json = {
              shareTitle: params?.h5Title || REDIRECT_TITLE,
              shareUrl: REDIRECT_URL,
              // shareUrl: `https://head.uni.changan.com.cn/quanzi/#/zixunPreview?artId=2076`,
              shareImg: REDIRECT_COVER,
              shareDesc: params?.h5Desc || REDIRECT_DESC,
              shareType: "3",
              isImg: "0",
            //   isMiniProgram: "1",
            //   userName: MINI_P_ID,
            //   path: `/pages/common/views/web-view/WebView?src=${encodeURIComponent(`${process.env.NEXT_PUBLIC_SERVER_BASE}${process.env.NEXT_PUBLIC_BASE_PATH}?sharephone=${user.phone}`)}`,
              // miniprogramType: 0,
            }
            try {
              // 菜单分享
              window.QYApp?.shareTo(JSON.stringify(json), 'shareCallBack')
            } catch (error) {
              Toast.show({content: `分享失败`})
              reject()
            }
    
          }
          // 小程序分享
          else {
            callFeedback({
              bodyClassName: 'share-tips-modal',
              actions: [],
              hideClose: true,
              title: <div >
              </div>,
              content: <img src={`${BASE_PATH}/img/game/share_indication.png`} width='308' />
            })
          }
          
          setTimeout(() => {
            requewt(API.user.share).then(()=> {
              // modal && modal.close()
              // Modal.clear()
              resolve()
              // global.location.reload()
            })
            // global.location.reload()
          }, 3000);
          })
    }
    const handleAppShareToMini = (params = {}) => {
        return new Promise((resolve,reject) => {
            // 记录分享
          if (env === `app`) {
            const searchParams = new URLSearchParams()
            Object.keys(params).forEach(key => {
                searchParams.append(key,params[key])
            })
            const path = `${process.env.NEXT_PUBLIC_SERVER_BASE}${process.env.NEXT_PUBLIC_BASE_PATH}?position=share&${searchParams.toString()}${REDIRECT_WX_PARAMS}`
            //
            const json = {
              shareTitle: REDIRECT_TITLE,
              shareUrl: REDIRECT_URL,
              // shareUrl: `https://head.uni.changan.com.cn/quanzi/#/zixunPreview?artId=2076`,
              shareImg: REDIRECT_COVER,
              shareDesc: REDIRECT_DESC,
              shareType: "3",
              isImg: "0",
              isMiniProgram: "1",
              userName: MINI_P_ID,
              path: `/pages/common/views/web-view/WebView?src=${encodeURIComponent(path)}`,
              // miniprogramType: 0,
            }
            try {
              // 菜单分享
              window.QYApp?.shareTo(JSON.stringify(json), 'shareCallBack')
            } catch (error) {
              Toast.show({content: `分享失败`})
              reject()
            }
    
          }
          // 小程序分享
          else {
            callFeedback({
              bodyClassName: 'share-tips-modal',
              actions: [],
              hideClose: true,
              title: <div >
              </div>,
              content: <img src={`${BASE_PATH}/img/game/share_indication.png`} width='308' />
            })
          }
          
          setTimeout(() => {
            requewt(API.user.share).then(()=> {
              // modal && modal.close()
              Modal.clear()
              resolve()
              // global.location.reload()
            })
            // global.location.reload()
          }, 3000);
          })
    }
    return {
        handleAppShareToMini,
        handleAppShareToH5
    }
}